package com.example.xxcjgl.ui.screens

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Scaffold
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.unit.dp
import com.example.xxcjgl.data.models.Resource
import com.example.xxcjgl.data.models.Student
import com.example.xxcjgl.viewmodel.StudentViewModel

// AddStudentScreen.kt
@Composable
fun AddStudentScreen(
    onBack: () -> Unit,
    viewModel: StudentViewModel = hiltViewModel()
) {
    var studentName by remember { mutableStateOf("") }
    var gender by remember { mutableStateOf("1") }
    var showError by remember { mutableStateOf(false) }
    var errorMessage by remember { mutableStateOf("") }

    val addStudentState by viewModel.addStudentState.collectAsState()

    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("添加学生") },
                navigationIcon = {
                    IconButton(onClick = onBack) {
                        Icon(Icons.Default.ArrowBack, contentDescription = "返回")
                    }
                },
                actions = {
                    IconButton(onClick = {
                        if (studentName.isBlank()) {
                            showError = true
                            errorMessage = "学生姓名不能为空"
                            return@IconButton
                        }

                        viewModel.addStudent(
                            Student(
                                xscjb_xh = 0,
                                xscjb_mc = studentName,
                                xscjb_xb = gender,
                                xscjb_sxcj = null,
                                xscjb_ywcj = null,
                                xscjb_csrrq = null,
                                xscjb_jmbz = 0,
                                xdcjb_jmmy = null
                            )
                        )
                    }) {
                        Icon(Icons.Default.Check, contentDescription = "保存")
                    }
                }
            )
        }
    ) { padding ->
        Column(
            modifier = Modifier
                .padding(padding)
                .fillMaxSize()
                .padding(16.dp)
        ) {
            if (showError) {
                Text(
                    text = errorMessage,
                    color = Color.Red,
                    modifier = Modifier.padding(bottom = 16.dp)
                )
            }

            OutlinedTextField(
                value = studentName,
                onValueChange = {
                    studentName = it
                    showError = false
                },
                label = { Text("学生姓名") },
                modifier = Modifier.fillMaxWidth()
            )

            Spacer(modifier = Modifier.height(16.dp))

            Text("性别", style = MaterialTheme.typography.bodyMedium)
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.Start
            ) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    RadioButton(
                        selected = gender == "1",
                        onClick = { gender = "1" }
                    )
                    Text("男")
                }

                Spacer(modifier = Modifier.width(16.dp))

                Row(verticalAlignment = Alignment.CenterVertically) {
                    RadioButton(
                        selected = gender == "0",
                        onClick = { gender = "0" }
                    )
                    Text("女")
                }
            }

            // 处理保存结果
            when (addStudentState) {
                is Resource.Success -> {
                    LaunchedEffect(Unit) {
                        onBack()
                    }
                }
                is Resource.Error -> {
                    val error = (addStudentState as Resource.Error).message
                    Text(
                        text = error ?: "保存失败",
                        color = Color.Red,
                        modifier = Modifier.padding(top = 16.dp)
                    )
                }
                is Resource.Loading -> {
                    CircularProgressIndicator(modifier = Modifier.padding(top = 16.dp))
                }
                else -> {}
            }
        }
    }
}